﻿using System.Linq;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Leo.Core.HttpApi.Host.SwaggerFilter
{
    public class HiddenSwaggerApiFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            //隐藏abp模块自带API
            foreach (var apiDescription in context.ApiDescriptions.Where(x => x.RelativePath.Contains("abp")))
            {
                string key = "/" + apiDescription.RelativePath;
                if (key.Contains("?"))
                {
                    int idx = key.IndexOf("?", System.StringComparison.Ordinal);
                    key = key.Substring(0, idx);
                }
                swaggerDoc.Paths.Remove(key);
            }
        }
    }
}
